package com.customview.UsmileCustomWave;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/12/30 10:23
 */
public class UsmileCustomWavem extends View {
    public UsmileCustomWavem(Context context) {
        this(context, null);
    }

    public UsmileCustomWavem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UsmileCustomWavem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    Paint mPaint;

    private void init() {

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);

        startAmin();
    }

    private void startAmin() {

        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, circleCount);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentCount = (int) animation.getAnimatedValue();
                postInvalidate();

            }
        });
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setDuration(3000);
        valueAnimator.start();
    }

    int DefHeight;
    int DefWidth;


    int mRadius;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        int width = getWidth();
        int height = getHeight();

        int min = Math.min(width, height);
        //去掉边距 再求半径
        mRadius = (min - BasePading * 2) / 2;
        init();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);


        switch (modeWidth) {

            case MeasureSpec.AT_MOST:
                DefWidth = dipToPx(getContext(), 80);
                break;
            case MeasureSpec.EXACTLY:
                DefWidth = sizeWidth;
                break;

        }
        switch (modeHeight) {

            case MeasureSpec.AT_MOST:
                DefHeight = dipToPx(getContext(), 80);
                break;
            case MeasureSpec.EXACTLY:
                DefHeight = sizeHeight;
                break;

        }

        setMeasuredDimension(DefWidth, DefHeight);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawCirlce(canvas);

    }


    int BasePading = dipToPx(getContext(), 24);

    int circleCount = 10;//总圈数
    int currentCount = 0;//当前圈数

    private void drawCirlce(Canvas canvas) {

        for (int i = 0; i < currentCount; i++) {
            int tmpradius = mRadius * i / circleCount;
            canvas.drawCircle(mRadius, mRadius, tmpradius, mPaint);
        }


    }

    /**
     * dip 转换成px
     *
     * @param dip
     * @return
     */
    public static int dipToPx(Context context, float dip) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1));
    }

}
